package com.suql.networktools.net


import com.google.gson.JsonSyntaxException
import com.suql.networktools.AppPreference
import com.suql.networktools.R
import com.suql.networktools.bean.TResponse
import com.suql.networktools.bean.TResponseNoData

import java.io.IOException
import java.net.SocketTimeoutException
import java.util.HashMap

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * Created by Calm on 2018/1/30.
 * RxHelper Rx单例类
 */

class RxHelper private constructor() {
    //    private HashMap<String, CompositeDisposable> mEventDisposable = new HashMap<>();
    private val mTaskDisposable = HashMap<String, CompositeDisposable>()

    private object RxHelperHolder {
        val instance = RxHelper()
    }

    companion object {

        val instance: RxHelper
            get() = RxHelperHolder.instance
    }

    private fun addTaskDisposable(tag: String, disposable: Disposable) {
        if (mTaskDisposable[tag] != null) {
            mTaskDisposable[tag]?.add(disposable)
        } else {
            val compositeDisposable = CompositeDisposable()
            compositeDisposable.add(disposable)
            mTaskDisposable[tag] = compositeDisposable
        }
    }

    fun <T> sendRequest(tag: String, observable: Observable<TResponse<T>>, onNext: Consumer<TResponse<T>>, onError: Consumer<Throwable>) {
        val disposable = observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onError)
        addTaskDisposable(tag, disposable)
    }

    fun sendRequestNoData(tag: String, observable: Observable<TResponseNoData>, onNext: Consumer<TResponseNoData>, onError: Consumer<Throwable>) {
        val disposable = observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext, onError)
        addTaskDisposable(tag, disposable)
    }

    fun unSubscribeTask(tag: String) {
        if (mTaskDisposable[tag] != null) {
            mTaskDisposable[tag]?.dispose()
            mTaskDisposable.remove(tag)
        }
    }

    fun unSubscribeTask(tag: String, d: Disposable) {
        if (mTaskDisposable[tag] != null) {
            mTaskDisposable[tag]?.remove(d)
            if (mTaskDisposable[tag]?.size()!! <= 0) {
                mTaskDisposable.remove(tag)
            }
        }
    }
    //    /**
    //     * 添加订阅
    //     *
    //     * @param tag
    //     * @param eventClass
    //     * @param onEvent
    //     * @return
    //     */
    //    public <E>Disposable subscribeEvent(String tag, Class<E> eventClass, final Consumer<E> onEvent)
    //    {
    //        Disposable subscription = RxBus2.getInstance().doSubscribe(eventClass,onEvent);
    //        if (mEventDisposable == null)
    //        {
    //            mEventDisposable = new HashMap<>();
    //        }
    //        if (mEventDisposable.get(tag) != null)
    //        {
    //            mEventDisposable.get(tag).add(subscription);
    //        } else
    //        {
    //            CompositeDisposable compositeDisposable = new CompositeDisposable();
    //            compositeDisposable.add(subscription);
    //            mEventDisposable.put(tag, compositeDisposable);
    //        }
    //        return subscription;
    //    }
    //
    //    /**
    //     * 取消tag下对应的Subscription
    //     *
    //     * @param tag
    //     * @param d
    //     */
    //    public void unsubscribeEvent(String tag, Disposable d)
    //    {
    //        if (mEventDisposable != null)
    //        {
    //            if (mEventDisposable.get(tag) != null)
    //            {
    //                mEventDisposable.get(tag).remove(d);
    //            }
    //        }
    //    }
    //
    //    /**
    //     * 取消tag对应的所有订阅
    //     *
    //     * @param tag
    //     */
    //    public void unsubscribeEvent(String tag)
    //    {
    //        if (mEventDisposable != null)
    //        {
    //            if (mEventDisposable.get(tag) != null)
    //            {
    //                mEventDisposable.get(tag).dispose();
    //                mEventDisposable.remove(tag);
    //            }
    //        }
    //    }

    fun getErrorInfo(throwable: Throwable): String {
        val errorInfo: String
        if (throwable is SocketTimeoutException) {
            errorInfo = AppPreference.getStringWithId(R.string.timeout_exception)
        } else if (throwable is IOException) {
            errorInfo = AppPreference.getStringWithId(R.string.network_error)
        } else if (throwable is JsonSyntaxException) {
            errorInfo = AppPreference.getStringWithId(R.string.data_format_error)
        } else {
            errorInfo = AppPreference.getStringWithId(R.string.default_request_error)
        }
        return errorInfo
    }


}
